#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class PagePermissionsProvider : CY.GFive.Core.Providers.DALProvider.IPagePermissionsProvider
	{
		#region Sql
		private static readonly string SqlSelectPagePermissions = "SELECT [Id], [PageKey],[PermissionsKey] FROM [PagePermissions] WHERE  [Id]=@Id";

		private static readonly string SqlInsertPagePermissions = "INSERT INTO [PagePermissions]([PageKey],[PermissionsKey]) values(@PageKey,@PermissionsKey);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdatePagePermissions = "UPDATE [PagePermissions] SET [PageKey] = @PageKey,[PermissionsKey] = @PermissionsKey WHERE  [Id]=@Id";

		private static readonly string SqlDeletePagePermissions = "DELETE [PagePermissions] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllPagePermissions = "SELECT [Id], [PageKey],[PermissionsKey] FROM [PagePermissions]";
		#endregion

		#region IPagePermissionsProvider Members
		public Core.Business.PagePermissions Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectPagePermissions);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.PagePermissions pagePermissions = new Core.Business.PagePermissions();

				if (!reader.IsDBNull(0)) pagePermissions.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) pagePermissions.PageKey = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) pagePermissions.PermissionsKey = reader.GetInt32(2);

				return pagePermissions;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.PagePermissions pagePermissions)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PageKey", SqlDbType.Int, pagePermissions.PageKey);
			sql.AddParameter("@PermissionsKey", SqlDbType.Int, pagePermissions.PermissionsKey);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertPagePermissions);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) pagePermissions.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.PagePermissions pagePermissions)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PageKey", SqlDbType.Int, pagePermissions.PageKey);
			sql.AddParameter("@PermissionsKey", SqlDbType.Int, pagePermissions.PermissionsKey);
			sql.AddParameter("@Id", SqlDbType.Int, pagePermissions.Id);
			sql.ExecuteSql(SqlUpdatePagePermissions);
		}

		public void Delete(Core.Business.PagePermissions pagePermissions)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, pagePermissions.Id);
			sql.ExecuteSql(SqlDeletePagePermissions);
		}

		public IList<Core.Business.PagePermissions> GetAllPagePermissions()
		{
			IList<Core.Business.PagePermissions> pagePermissionslist = new List<Core.Business.PagePermissions>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllPagePermissions);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.PagePermissions pagePermissions = new Core.Business.PagePermissions();

					if (!reader.IsDBNull(0)) pagePermissions.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) pagePermissions.PageKey = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) pagePermissions.PermissionsKey = reader.GetInt32(2);

					pagePermissions.MarkOld();
					pagePermissionslist.Add(pagePermissions);
				}
				reader.Close();
			}
			return pagePermissionslist;
		}
		#endregion
	}
}
